/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package modules.estoque.models.dao;

/**
 *
 * @author Clezio
 */
import java.util.List;
import modules.estoque.models.dto.Permissao;
import modules.estoque.util.HibernateUtil;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
   
 public class PermissaoDAO{  
     
     private SessionFactory sessionFactory;
     
     public PermissaoDAO() throws Exception{
         sessionFactory = HibernateUtil.getSessionFactory();
     }
       
     public void save(Permissao permissao) throws Exception{
         Session session = sessionFactory.openSession();
         Transaction tx = session.beginTransaction();
         if(permissao.getId() > 0){
            session.update(permissao);
         }else{
            session.save(permissao);
         }
         session.flush();
         tx.commit();
         session.close();  
     }  
       
     public java.util.List getList(String condicao) throws Exception{  
         Session session = sessionFactory.openSession();
         Query query = session.createQuery(condicao);
         List permissoes = query.list();
         session.flush();  
         session.close();  
         return permissoes;
     }  
       
     public Permissao retrieve(Integer pk) throws Exception{
         Session session = sessionFactory.openSession();
         Permissao permissao = (Permissao)session.load(Permissao.class, pk);
         Permissao permissaoDTO = mountDto(permissao);
         session.flush();
         session.close();  
         return permissaoDTO;
     }  
       
     public void delete(Permissao Permissao) throws Exception{
         Session session = sessionFactory.openSession();
         Transaction tx = session.beginTransaction();
         session.delete(Permissao);
         session.flush();
         tx.commit();
         session.close();  
     }

    private Permissao mountDto(Permissao permissao) {
        Permissao permissaoDTO = new Permissao();
        permissaoDTO.setId(permissao.getId());
        permissaoDTO.setNome(permissao.getNome());
        permissaoDTO.setAtivo(permissao.isAtivo());
        permissaoDTO.setGruposAcessosPermissoeses(permissao.getGruposAcessosPermissoeses());
        return permissaoDTO;
    }
 }